﻿using System;
using System.Web;
using System.Web.Mvc;
using MvcContrib.PortableAreas;
using OpenIdPortableArea.Messages;
using OpenIdPortableArea.Helpers;

namespace MvcApplication1.Services.PortableAreaHandlers
{
    public class AuthenticatedMessageHandler : MessageHandler<AuthenticatedMessage>
    {
        public override void Handle(AuthenticatedMessage message)
        {
            const string openid = "http://johncoder.myopenid.com/";

            if (message.ClaimedIdentifier == openid)
            {
                OpenIdHelpers.Login("John Nelson", "jnelson@johncoder.com", new TimeSpan(0, 5, 0), true);
            }
            else
            {
                if (message.ClaimsResponse != null)
                    HttpContext.Current.Session.Add("OpenIDClaimedEmail", message.ClaimsResponse.Email ?? string.Empty);

                HttpContext.Current.Session.Add("OpenIDClaimedIdentifier", message.ClaimedIdentifier);
                
                message.Result = new RedirectResult("~/Home/Register");
            }
        }
    }
}